import { useUserHooks } from '@/stores/user'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/chat/list',
      name: 'ChatList',
      meta: {
        title: '问诊列表'
      },
      component: () => import('@/views/chatList/indexView.vue')
    },
    {
      path: '/chat/:id',
      name: 'Chat',
      meta: {
        title: '聊天'
      },
      component: () => import('@/views/chat/indexView.vue')
    },
    {
      path: '/login',
      name: 'Login',
      meta: {
        title: '登录'
      },
      component: () => import('@/views/login/indexView.vue')
    },
    {
      path: '/register',
      name: 'Register',
      meta: {
        title: '注册'
      },
      component: () => import('@/views/register/indexView.vue')
    },
    {
      path: '/my',
      name: 'My',
      meta: {
        title: '我的'
      },
      component: () => import('@/views/my/indexView.vue')
    },
    {
      path: '/dis',
      name: 'Dis',
      meta: {
        title: '病友圈'
      },
      component: () => import('@/views/dis/indexView.vue')
    },
    {
      path: '/',
      redirect: '/my'
    }
  ]
})

router.beforeEach((to) => {
  document.title = to.meta.title as string
  const whiteList = ['/login', '/register']
  const isLogin = useUserHooks().isLogin
  if (!isLogin && !whiteList.includes(to.fullPath)) {
    return '/login'
  }
})

export default router
